package com.xinghen.security.core.validate.code.sms;

import com.xinghen.security.core.properties.SecurityProperties;
import com.xinghen.security.core.validate.code.ValidateCode;
import com.xinghen.security.core.validate.code.ValidateCodeGenerator;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * @author xinghen
 * @ClassName: SmsValidateCodeGenerator
 * @Package: com.xinghen.security.core.validate.code.sms
 * @Description 短信验证码生成器
 * @create 2018/4/18 15:57
 */
@Component
public class SmsValidateCodeGenerator implements ValidateCodeGenerator {

    @Autowired
    private SecurityProperties securityProperties;

    /**
     * 生成短信验证码
     *
     * @param request
     * @return
     */
    @Override
    public ValidateCode generate(ServletWebRequest request) {
        String code = RandomStringUtils.randomNumeric(securityProperties.getCode().getSms().getLength());
        return new ValidateCode(code, securityProperties.getCode().getSms().getExpireIn());
    }
}
